package cn.skyisazure.metacompare.sqlcompare.meta;

import cn.hutool.core.util.StrUtil;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.Objects;

/**
 * 索引信息
 *
 * @Author lizhuyang
 */
@Data
@Accessors(chain = true)
public class IndexCM {

    private Serializable id;

    private String tableName;
    private String tableId;
    private String indexName;
    // 是否唯一索引（unique）
    private Boolean uniqueFlag;
    private String indexType;
    private String indexComment;
    // 原值：id,deleteAt
    private String columnNames;

    /** 旧索引名（修改索引时设置此属性） */
    private String oldName;

    /* 返回： [`id` ASC,`deleteAt` ASC]*/
    public String getAssembColumnNames() {
        if (StrUtil.isNotBlank(columnNames)){
            // [id,deleteAt]
            String[] orgColNames = columnNames.split(",");
            String[] colNames = new String[orgColNames.length];
            for (int i = 0; i < orgColNames.length; i++) {
                colNames[i]  = "`"+orgColNames[i]+"` ASC";
            }
            return String.join(",", colNames);
        }
        return columnNames;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {return true;}
        if (o == null || getClass() != o.getClass()) {return false;}
        IndexCM indexCM = (IndexCM) o;
        return  Objects.equals(indexName, indexCM.indexName)
                && Objects.equals(uniqueFlag, indexCM.uniqueFlag)
                && Objects.equals(indexType, indexCM.indexType)
                && Objects.equals(indexComment, indexCM.indexComment)
                && Objects.equals(columnNames, indexCM.columnNames);
    }

    @Override
    public int hashCode() {
        return Objects.hash(indexName, uniqueFlag, indexType, indexComment, columnNames);
    }
}
